/**
 * 
 */
package cn.gov.msa.ws;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.aspectj.weaver.ast.Var;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.File;
import cn.gov.msa.model.Notice;
import cn.gov.msa.model.Question;
import cn.gov.msa.model.Repository;
import cn.gov.msa.model.ServiceGuide;
import cn.gov.msa.util.GsonHelper;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 *
 */

public class NoticeServcie {
	
	
	public static String allList(ApplicationContext ctx,String params) throws Exception {
		String result = "";
	    JSONObject jsonResult = new JSONObject();
	    cn.gov.msa.service.NoticeService noticeService = (cn.gov.msa.service.NoticeService) ctx.getBean("NoticeService");  				        
		JSONObject jsonParams = JSONObject.fromObject(params);
		int pageNum = 1;
		int pageSize = 10;
		if(jsonParams.has("pageNum")){
			pageNum = jsonParams.getInt("pageNum");
			jsonParams.remove("pageNum");
		}
		if(jsonParams.has("pageSize")){
			pageSize = jsonParams.getInt("pageSize");
			jsonParams.remove("pageSize");
		}	
		Notice notice = GsonHelper.convertEntity(jsonParams.toString(), Notice.class);
		List<Notice> noticeList = noticeService.selectAllData(pageNum, pageSize ,notice);
		Date myDate = new Date();
		
		for (Notice noticeItem : noticeList) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String str = sdf.format(noticeItem.getPublishTime());
			noticeItem.setPu(str);
			noticeItem.setIsDue(noticeItem.getAsofDate().compareTo(myDate)<0);
		}
		JSONArray jaResult = JSONArray.fromObject(noticeList);
		// 获取分页信息
		PageInfo page = new PageInfo(noticeList);
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", jaResult);		
		return jsonResult.toString();
	}

	
	public static String list(ApplicationContext ctx,String param) throws Exception {
		String result = "";
		
		cn.gov.msa.service.NoticeService noticeService = (cn.gov.msa.service.NoticeService) ctx.getBean("NoticeService");  	
		int pageNum = 1;
		int pageSize = 5;
		List<Notice> noticeList = noticeService.selectwaiData(pageNum, pageSize);
		for (Notice notice : noticeList) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String str = sdf.format(notice.getPublishTime());
			notice.setPu(str);
		}
		JSONArray ja = JSONArray.fromObject(noticeList);
				
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("rows", ja);
				
		result = jsonResult.toString();
		return result;
	}
	
	
	public static String info(ApplicationContext ctx,String params) throws Exception {
		JSONObject jsonResult = new JSONObject();
		cn.gov.msa.service.NoticeService noticeService = (cn.gov.msa.service.NoticeService) ctx.getBean("NoticeService");  	  				        
		JSONObject jsonParams = JSONObject.fromObject(params);
		
		
		Notice notice = GsonHelper.convertEntity(jsonParams.toString(), Notice.class);
		List<Notice> noticeList=noticeService.selectNotice(1, 1, notice);
	    JSONArray jaResult = JSONArray.fromObject(noticeList);

		jsonResult.put("rows", jaResult);	
		
		return jsonResult.toString();
	}
	
	
}
